//
//  WabViewController.m
//  10-10
//
//  Created by 钟纪旺 on 16/11/21.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import "WabViewController.h"

@interface WabViewController ()<UIWebViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *test;
@property (weak, nonatomic) IBOutlet UIWebView *wab;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *av;

@end

@implementation WabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//    self.title = @"百度";
//    NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
//    NSURLRequest *req = [NSURLRequest requestWithURL:url];
//    self.wab.delegate =self;
//    self.test.delegate =self;
//    [self.wab loadRequest:req];
//    [self.av startAnimating];
    _test.clearButtonMode = UITextFieldViewModeWhileEditing;
    _test.delegate =self;
    _wab.delegate =self;
    [_av startAnimating];
    _test.keyboardType = UIKeyboardTypeURL;
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest *reqest = [NSURLRequest requestWithURL:url];
    [_wab loadRequest:reqest];
    
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *back =[[UIBarButtonItem alloc]initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(onBack:)];
    UIBarButtonItem *forward = [[UIBarButtonItem alloc]initWithTitle:@">" style:UIBarButtonItemStylePlain target:self action:@selector(onForward:)];
    self.toolbarItems = @[back,forward,space];
    
}
- (void)onBack:(id)sender{

    if ([_wab canGoBack]) {
        [_wab goBack];
    }

}
- (void)onForward:(id)sender{
    if ([_wab canGoForward]) {
        [_wab goForward];
    }


}
- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.toolbarHidden =NO;
}
-(void)viewDidDisappear:(BOOL)animated{

    self.navigationController.toolbarHidden =YES;

}
- (void)webViewDidStartLoad:(UIWebView *)webView{

}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self.av stopAnimating];

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    [self.av stopAnimating];

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",textField.text]]];
    [_wab loadRequest:request];

    return YES;

}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
